package concole;

/**
 * 
 * @author LiuJing This class will also ensure the input format of the commandF
 *         and if some information is null,will give the hint
 * 
 */
public class CommandHelper {

	public static int getAdminCmd(String cmdStr) {
		String[] string = cmdStr.split(" ");
		int adminCmdInt = 10;
		if (string[0].equals("ChangePassword")) {
			adminCmdInt = 0;
		} else if (string[0].equals("Show")) {
			adminCmdInt = 1;
		} else if (string[0].equals("Delete")) {
			adminCmdInt = 2;
		} else if (string[0].equals("Exit")) {
			adminCmdInt = 3;
		} else if (string[0].equals("SelectCourse")) {
			adminCmdInt = 4;
		}
		return adminCmdInt;
	}

	public static int getTeacherCmd(String cmdStr) {
		String[] string = cmdStr.split(" ");
		int teacherCmdInt = 0;
		if (string[0].equals("Exit")
				|| (string.length != 3 && !string[0].equals("Publish"))
				|| (string.length == 3 && ((!string[2].endsWith("]") || !string[2]
						.startsWith("["))))) {
			if (string[0].equals("Exit")) {
				return 0;
			} else {
				return 6;
			}
		} else if (string[0].equals("Publish")) {
			teacherCmdInt = 1;
		} else if (string[0].equals("Show") && string[1].equals("course")) {
			teacherCmdInt = 2;
		} else if (string[0].equals("Update") && string[1].equals("course")) {
			teacherCmdInt = 3;
		} else if (string[0].equals("Show") && string[1].equals("student")) {
			teacherCmdInt = 4;
		} else if (string[0].equals("Record") && string[1].equals("score")) {
			teacherCmdInt = 5;
		} else {
			teacherCmdInt = 6;
		}
		return teacherCmdInt;
	}

	public static int getStudentCmd(String cmdStr) {
		String[] cmdStrings = cmdStr.split(" ");
		for (int i = 0; i < cmdStrings.length; i++) {
			cmdStrings[i] = cmdStrings[i].trim();
		}
		int studentCmd = 0;
		if (cmdStrings.length == 1 && cmdStrings[0].equals("Exit")) {
			studentCmd = 0;
		} else if (cmdStrings.length == 2 && cmdStrings[0].equals("Show")
				&& cmdStrings[1].equals("mycourseList")) {
			studentCmd = 1;
		} else if (cmdStrings.length == 3) {
			if (cmdStrings[2].matches("\\[.(.)*\\]")) {
				if (cmdStrings[0].equals("Select")
						&& cmdStrings[1].equals("course")) {
					studentCmd = 2;
				} else if (cmdStrings[0].equals("Quit")
						&& cmdStrings[1].equals("course")) {
					studentCmd = 3;
				} else if (cmdStrings[0].equals("Show")
						&& cmdStrings[1].equals("score")) {
					studentCmd = 4;
				} else {
					studentCmd = 5;
				}
			} else {
				System.out.println("sorry the format is wrong");
				studentCmd = 6;
			}
		} else {
			System.out.println("Sorry the command not allowed");
			studentCmd = 6;
		}
		return studentCmd;
	}

	public static int getOperand(String cmdString) {
		int cmdInt = 0;
		System.out.println("getOperand cmdStr:" + cmdString);
		String[] cmdStrings = cmdString.split("!");
		if (cmdStrings[0].trim().equals("Login")) {
			/* the number begin with 1 will represent login operation */
			if (cmdStrings[1].trim().equals("0")) {
				cmdInt = 10;
			} else if (cmdStrings[1].trim().equals("1")) {
				cmdInt = 11;
			} else {
				cmdInt = 12;
			}
			return cmdInt;
		} else if (cmdStrings[0].trim().equals("AdminOp")) {
			// the number begin with 2 will represent operation of administrator
			if (cmdStrings[1].trim().equals("1")) {
				cmdInt = 21;
			} else if (cmdStrings[1].trim().equals("2")) {
				cmdInt = 22;
			} else {
				cmdInt = 23;
			}
		} else if (cmdStrings[0].equals("TeacherOp")) {
			// the number begin with 3 will represent operation of teacher
			String string = "3";
			string += cmdStrings[1];
			cmdInt = Integer.parseInt(string);
			System.out.println(cmdInt);
		} else if (cmdStrings[0].equals("StudentOp")) {
			// the number begin with 4 will represent operation of student
			String string = "4";
			string += cmdStrings[1];
			cmdInt = Integer.parseInt(string);
		}
		return cmdInt;
	}

	public static String getOpcode(String cmdString) {
		String opcodeString = "";
		String[] cmdStrings = cmdString.split("!");
		int cmdInt = CommandHelper.getOperand(cmdString);
		switch (cmdInt) {
		case 10:

		case 11:

		case 12:
			opcodeString = cmdStrings[2].trim();
			break;
		case 21:
			opcodeString = cmdStrings[2].trim();
			break;
		case 22:
			opcodeString = cmdStrings[2].trim();
			break;
		case 23:
			opcodeString = cmdStrings[2].trim() + "!" + cmdStrings[3].trim();
			break;
		case 30:
			opcodeString=cmdStrings[2].trim();
			break;
		case 31:
			opcodeString = cmdStrings[2] + "!" + cmdStrings[3] + "!"
					+ cmdStrings[4] + "!" + cmdStrings[5];
			break;
		case 32:
			opcodeString = cmdStrings[2];
			System.out.println("32");
			break;
		case 33:
			opcodeString = cmdStrings[2];
			System.out.println("33");
			break;
		case 34:
			opcodeString = cmdStrings[2];
			System.out.println("34");
			break;
		case 35:
			opcodeString = cmdStrings[2] + "!" + cmdStrings[3];
			System.out.println("35");
			break;
		case 36:
			opcodeString = cmdStrings[2];
			System.out.println("36");
			break;
		case 37:
			opcodeString = cmdStrings[2];
			System.out.println(opcodeString);
			System.out.println("37");
			break;
		case 38:
			opcodeString = cmdStrings[2];
			System.out.println(opcodeString);
			System.out.println("38");
			break;
		case 39:
			opcodeString = cmdStrings[2].trim();
			System.out.println("39"+opcodeString);
			break;
		case 310:
			opcodeString=cmdStrings[2].trim();
			System.out.println("In get opcode:39"+opcodeString);
			break;
		case 41:
			opcodeString = cmdStrings[2];
			System.out.println("41");
			break;
		case 42:
			opcodeString = cmdStrings[2] + "!" + cmdStrings[3];
			System.out.println("42");
			break;
		case 43:
			opcodeString = cmdStrings[2] + "!" + cmdStrings[3];
			System.out.println("43");
			break;
		case 44:
			opcodeString = cmdStrings[2] + "!" + cmdStrings[3];
			System.out.println("44");
			break;
		case 45:
			opcodeString = cmdStrings[2] + "!" + cmdStrings[3];
			System.out.println("45");
			break;
		case 46:
			opcodeString = cmdStrings[2];
			System.out.println("46");
			break;
		case 47:
			opcodeString=cmdStrings[2];
			System.out.println("47");
		default:
			break;
		}
		return opcodeString;
	}
}
